package geekbang.week2.submit;

import com.sun.jna.platform.FileUtils;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;

/**
 *普通的GET请求
 */
public class Task6 {
    public static void main(String[] args) throws Exception {
        doGet();
    }

    public static void doGet()  throws Exception {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建http GET请求
        HttpGet httpGet = new HttpGet("http://127.0.0.1:8801");
        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if ( 200==response.getCode()) {
                //请求体内容
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");

                System.out.println("内容长度："+content.length());
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            //相当于关闭浏览器
            httpclient.close();
        }
    }
}